package idiet.view;

import idiet.Activator;
import idiet.bean.Customer;
import idiet.bean.CustomerNurse;
import idiet.bean.FoodIngredientElement;
import idiet.bean.FoodMaterial;
import idiet.bean.FoodMaterialCategory;
import idiet.bean.MealRecipe;
import idiet.bean.Rni;
import idiet.bean.Unit;
import idiet.chart.Padding;
import idiet.service.CustomerNurseService;
import idiet.service.CustomerService;
import idiet.service.FoodMaterialCategoryService;
import idiet.service.FoodMaterialService;
import idiet.service.MealService;
import idiet.service.RniService;
import idiet.service.UnitService;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class ViewPartPantryCustomize extends ViewPart {
	private static Logger logger=Logger.getLogger(ViewPartPantryCustomize.class);
	private List<Customer> customers=new ArrayList<Customer>();
	private CustomerService customerService=Activator.getApplicationContext().getBean(CustomerService.class);
	private CustomerNurseService customerNurseService=Activator.getApplicationContext().getBean(CustomerNurseService.class);
	private FoodMaterialCategoryService materialCategroyService=Activator.getApplicationContext().getBean(FoodMaterialCategoryService.class);
	private FoodMaterialService foodMaterialService=Activator.getApplicationContext().getBean(FoodMaterialService.class);
	private UnitService unitService=Activator.getApplicationContext().getBean(UnitService.class);
	private MealService mealService=Activator.getApplicationContext().getBean(MealService.class);
	private RniService rniService=Activator.getApplicationContext().getBean(RniService.class);
	private Text textCustomerName;
	private Text textMaterial;
	private Table table;
	private Table tableChosedMaterial;
	private Table tableMaterial;
	private DateTime dateTimeStart;
	private DateTime dateTimeEnd;
	private TableViewer tableViewer;
	private TableViewer tableViewerRni;
	private Combo comboPrimaryCategory;
	private Combo comboSecondaryCategory;
	private List<FoodMaterial> materials=new ArrayList<FoodMaterial>();
	private List<MealRecipe> recipe=new ArrayList<MealRecipe>();
	private List<Rni> rnis=null;
	private CustomerNurse currentCustomerNurse;
	private CheckboxTableViewer checkboxTableViewer;
	private CheckboxTableViewer recipeCheckboxTableViewer;
	private Canvas canvasResult;
	public ViewPartPantryCustomize() {
		// TODO Auto-generated constructor stub
	}
	public static String getId(){
		return "iDiet.view.customize.pantry";
	}
	public static final String ID = "iDiet.view.meal";
	private Table tableRni;
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(4, false));
		
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(new GridLayout(1, false));
		GridData gd_group = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_group.widthHint = 226;
		group.setLayoutData(gd_group);
		
		Composite composite = new Composite(group, SWT.NONE);
		composite.setLayout(new GridLayout(5, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_composite.heightHint = 63;
		composite.setLayoutData(gd_composite);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Nurse Date");
		
		dateTimeStart = new DateTime(composite, SWT.BORDER);
		GridData gd_dateTime_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_dateTime_1.widthHint = 100;
		dateTimeStart.setLayoutData(gd_dateTime_1);
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("-");
		
		dateTimeEnd = new DateTime(composite, SWT.BORDER);
		GridData gd_dateTime = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_dateTime.widthHint = 97;
		dateTimeEnd.setLayoutData(gd_dateTime);
		new Label(composite, SWT.NONE);
		
		Label lblCustomerName = new Label(composite, SWT.NONE);
		lblCustomerName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblCustomerName.setText("Customer Name");
		
		textCustomerName = new Text(composite, SWT.BORDER);
		textCustomerName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnNewButton.widthHint = 63;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("Query");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SimpleDateFormat format=new SimpleDateFormat("M/d/yyyy");
				//customer.setForeStartDate(format.parse(StringUtils.substringBetween(dateTimeStart.toString(), "{", "}")));
				if(textCustomerName.getText().trim().equals("")){
					
					if(dateTimeStart.toString().equals(dateTimeEnd.toString())){
						customers=customerService.getCustomers(null);
					}else{
						try {
							customers=customerService.getCustomers(null, new Timestamp(format.parse(StringUtils.substringBetween(dateTimeStart.toString(), "{", "}")).getTime()), new Timestamp(format.parse(StringUtils.substringBetween(dateTimeEnd.toString(), "{", "}")).getTime()));
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}else{
					if(dateTimeStart.toString().equals(dateTimeEnd.toString())){
						customers=customerService.getCustomers(textCustomerName.getText());
					}else{
						try {
							customers=customerService.getCustomers(textCustomerName.getText(), new Timestamp(format.parse(StringUtils.substringBetween(dateTimeStart.toString(), "{", "}")).getTime()), new Timestamp(format.parse(StringUtils.substringBetween(dateTimeEnd.toString(), "{", "}")).getTime()));
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
				tableViewer.setInput(customers);
			}
		});
		tableViewer = new TableViewer(group, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		table.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				IStructuredSelection selections =(IStructuredSelection)tableViewer.getSelection();
				Customer customer=(Customer)selections.getFirstElement();
				if(customer!=null){
					CustomerNurse customerNurse=customerNurseService.getCustomerNurse4LastByCustomer(customer);
					currentCustomerNurse=customerNurse;
					rnis=rniService.getRnis(customerNurse.getAge());
					tableViewerRni.setInput(rnis);
				}
			}
		});
		tableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("Name");
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getName();
			}
		});
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("Gender");
		tableViewerColumn_3.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getGender();
			}
		});
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("Nation");
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getNation().getName();
			}
		});
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("Phone");
		tableViewerColumn_4.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element){
				return ((Customer)element).getTelephone();
			}
		});
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("Cellphone");
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getCellphone();
			}
		});
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_5.setWidth(100);
		tblclmnNewColumn_5.setText("Im");
		tableViewerColumn_5.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getIm();
			}
		});
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_6.setWidth(100);
		tblclmnNewColumn_6.setText("Adress");
		tableViewerColumn_6.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((Customer)element).getAddress();
			}
		});
		
		Group group_1 = new Group(parent, SWT.NONE);
		group_1.setLayout(new GridLayout(3, false));
		GridData gd_group_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1);
		gd_group_1.widthHint = 472;
		group_1.setLayoutData(gd_group_1);
		
		Composite composite_1 = new Composite(group_1, SWT.NONE);
		composite_1.setLayout(new GridLayout(7, false));
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1);
		gd_composite_1.heightHint = 35;
		gd_composite_1.widthHint = 436;
		composite_1.setLayoutData(gd_composite_1);
		
		Label lblNewLabel_5 = new Label(composite_1, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_5.setText("Category");
		
		comboPrimaryCategory = new Combo(composite_1, SWT.NONE);
		comboPrimaryCategory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<FoodMaterialCategory> primaryCategories=materialCategroyService.getParentFoodMaterialCategories();
		for(FoodMaterialCategory category:primaryCategories){
			comboPrimaryCategory.add(category.getName());
			comboPrimaryCategory.setData(category.getName(),category.getId());
		}
		comboPrimaryCategory.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				String selection=comboPrimaryCategory.getText();
				if(!selection.trim().equals("")){
					comboSecondaryCategory.setText("");
					
					FoodMaterialCategory parent=new FoodMaterialCategory();
					parent.setId(new Integer(comboPrimaryCategory.getData(selection).toString()));
					List<FoodMaterialCategory> secondaryCategories=materialCategroyService.getChildFoodMaterialCategories(parent);
					for(FoodMaterialCategory category:secondaryCategories){
						comboSecondaryCategory.add(category.getName());
						comboSecondaryCategory.setData(category.getName(),category.getId());
					}
					/*
					 * input checkbox table viewer
					 */
					materials=foodMaterialService.getFoodMaterials(parent, true);
					checkboxTableViewer.setInput(materials);
					//checkboxTableViewer.setAllGrayed(true);
					//checkboxTableViewer.setAllChecked(true);
					//checkboxTableViewer.refresh();
				}
			}
		});
		
		Label lblNewLabel_6 = new Label(composite_1, SWT.NONE);
		lblNewLabel_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_6.setText("Sub Category");
		
		comboSecondaryCategory = new Combo(composite_1, SWT.NONE);
		comboSecondaryCategory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		comboSecondaryCategory.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				String selection=comboSecondaryCategory.getText();
				if(!selection.trim().equals("")){
					FoodMaterialCategory secondary=new FoodMaterialCategory();
					secondary.setId(new Integer(comboSecondaryCategory.getData(selection).toString()).intValue());
					materials=foodMaterialService.getFoodMaterials(secondary,false);
					checkboxTableViewer.setInput(materials);
					
				}
			}
		});
		
		Label lblNewLabel_7 = new Label(composite_1, SWT.NONE);
		lblNewLabel_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_7.setText("Material");
		
		textMaterial = new Text(composite_1, SWT.BORDER);
		textMaterial.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnNewButtonQuery = new Button(composite_1, SWT.NONE);
		btnNewButtonQuery.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String primaryCategory=comboPrimaryCategory.getText().trim().equals("")?null:comboPrimaryCategory.getText().trim();
				String secondaryCategory=comboSecondaryCategory.getText().trim().equals("")?null:comboSecondaryCategory.getText().trim();
				String materialName=textMaterial.getText().trim().equals("")?null:textMaterial.getText().trim();
				if(primaryCategory!=null){
					FoodMaterialCategory category=new FoodMaterialCategory();
					if(secondaryCategory!=null){
						category.setId(new Integer(comboSecondaryCategory.getData(secondaryCategory).toString()));
						materials=foodMaterialService.getFoodMaterials(materialName,category, false);
					}else{
						category.setId(new Integer(comboPrimaryCategory.getData(primaryCategory).toString()));
						materials=foodMaterialService.getFoodMaterials(materialName,category, true);
					}
				}else{
					materials=foodMaterialService.getFoodMaterials(materialName);
				}
				checkboxTableViewer.setInput(materials);
				
			}
		});
		btnNewButtonQuery.setText("Query");
		
		Group group_5 = new Group(group_1, SWT.NONE);
		group_5.setLayout(new BorderLayout(0, 0));
		group_5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		checkboxTableViewer = CheckboxTableViewer.newCheckList(group_5, SWT.BORDER | SWT.FULL_SELECTION);
		tableMaterial = checkboxTableViewer.getTable();
		tableMaterial.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				/*TableItem[] tableItems=tableMaterial.getSelection();
				TableItem tableItem=tableItems[0];
				tableItem.setGrayed(true);*/
				StructuredSelection selection =(StructuredSelection) checkboxTableViewer.getSelection();
				//int[] selectionIndex=checkboxTableViewer.getTable().getSelectionIndices();
				Object o=selection.getFirstElement();
				//if(!checkboxTableViewer.getChecked(o)){
				//	checkboxTableViewer.setChecked(o, true);
					FoodMaterial m=(FoodMaterial)o;
					MealRecipe recipeItem=new MealRecipe();
					recipeItem.setFoodMaterial(m);
					//recipeItem.setVersion(0);
					recipeItem.setQty(new BigDecimal(100));
					Unit unit=new Unit();
					unit.setId(1);
					recipeItem.setUnit(unit);
					recipe.add(recipeItem);
					//recipeCheckboxTableViewer.add(recipeItem);
					recipeCheckboxTableViewer.setInput(recipe);
					
					checkboxTableViewer.remove(o);
					recipeCheckboxTableViewer.refresh();
					
				//}
				//MessageDialog.openInformation(getSite().getShell(), "dddd",m.getName() );
			}
		});
		tableMaterial.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		tableMaterial.setLinesVisible(true);
		tableMaterial.setHeaderVisible(true);
		tableMaterial.setLayoutData(BorderLayout.CENTER);
		checkboxTableViewer.setContentProvider(new ArrayContentProvider());
		//checkboxTableViewer.setSorter(new ViewerSorter());
		checkboxTableViewer.setComparator(new ViewerComparator(){

			@Override
			public int category(Object element) {
				// TODO Auto-generated method stub
				return super.category(element);
			}

			@Override
			public int compare(Viewer viewer, Object e1, Object e2) {
				// TODO Auto-generated method stub
				FoodMaterial m1 = (FoodMaterial) e1;  
				FoodMaterial m2 = (FoodMaterial) e2;  
		        int rc = 0;  
		        rc=m1.getName().compareTo(m2.getName());
		        /*switch (propertyIndex) {  
		        case 0:  
		            rc = p1.getFirstName().compareTo(p2.getFirstName());  
		            break;  
		        case 1:  
		            rc = p1.getLastName().compareTo(p2.getLastName());  
		            break;  
		        default:  
		            rc = 0;  
		        } */ 
		        // If descending order, flip the direction  
		        if (tableMaterial.getSortDirection() == SWT.DOWN) {  
		            rc = -rc;  
		        }  
		        return rc; 
				//return super.compare(viewer, e1, e2);
			}

			@Override
			public boolean isSorterProperty(Object element, String property) {
				// TODO Auto-generated method stub
				return super.isSorterProperty(element, property);
			}

			@Override
			public void sort(Viewer viewer, Object[] elements) {
				// TODO Auto-generated method stub
				super.sort(viewer, elements);
			}
		});
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		final TableColumn tblclmnNewColumn_7 = tableViewerColumn_7.getColumn();
		tblclmnNewColumn_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//tableViewerColumn.
				//e.
				tableMaterial.setSortColumn(tblclmnNewColumn_7);
				if(tableMaterial.getSortDirection()==SWT.UP)
					tableMaterial.setSortDirection(SWT.DOWN);
				else
					tableMaterial.setSortDirection(SWT.UP);
				//MessageDialog.openInformation(getSite().getShell(), "dddd","kkkkkkkkkkk");
			}
		});
		tblclmnNewColumn_7.setWidth(250);
		tblclmnNewColumn_7.setText("Food Material");
		tableViewerColumn_7.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((FoodMaterial)element).getName();
			}
			
		});
		Composite composite_2 = new Composite(group_1, SWT.NONE);
		GridData gd_composite_2 = new GridData(SWT.CENTER, SWT.FILL, false, false, 1, 1);
		gd_composite_2.widthHint = 40;
		composite_2.setLayoutData(gd_composite_2);
		composite_2.setLayout(new GridLayout(1, false));
		new Label(composite_2, SWT.NONE);
		
		Button btnNewButtonSingle = new Button(composite_2, SWT.NONE);
		btnNewButtonSingle.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection =(StructuredSelection) checkboxTableViewer.getSelection();
				if(selection.isEmpty())
					return;
				//Object[]  selections =checkboxTableViewer.getCheckedElements();
				Object o=selection.getFirstElement();
				//if(selections.length>0){
					//Object o=selections[0];
					FoodMaterial m=(FoodMaterial)o;
					MealRecipe recipeItem=new MealRecipe();
					recipeItem.setFoodMaterial(m);
					recipeItem.setQty(new BigDecimal(100));
					Unit unit=new Unit();
					unit.setId(1);
					recipeItem.setUnit(unit);
					recipe.add(recipeItem);
					recipeCheckboxTableViewer.setInput(recipe);
					
					checkboxTableViewer.remove(o);
					recipeCheckboxTableViewer.refresh();
				//}
			}
		});
		btnNewButtonSingle.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnNewButtonSingle.setText(">");
		
		Button btnNewButtonMulti = new Button(composite_2, SWT.NONE);
		btnNewButtonMulti.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Object[]  selections =checkboxTableViewer.getCheckedElements();
				for(Object o:selections){
					//Object o=selections[0];
					FoodMaterial m=(FoodMaterial)o;
					MealRecipe recipeItem=new MealRecipe();
					recipeItem.setFoodMaterial(m);
					recipeItem.setQty(new BigDecimal(100));
					Unit unit=new Unit();
					unit.setId(1);
					recipeItem.setUnit(unit);
					recipe.add(recipeItem);
					checkboxTableViewer.remove(o);
				}
				recipeCheckboxTableViewer.setInput(recipe);
				recipeCheckboxTableViewer.refresh();
			}
		});
		btnNewButtonMulti.setText(">>");
		
		Button btnNewButtonMultiUnchose = new Button(composite_2, SWT.NONE);
		btnNewButtonMultiUnchose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Object[] selections=recipeCheckboxTableViewer.getCheckedElements();
				for(Object o:selections){
					MealRecipe recipeItem=(MealRecipe)o;
					FoodMaterial m=recipeItem.getFoodMaterial();
					checkboxTableViewer.add(m);
					recipe.remove(recipeItem);
					recipeCheckboxTableViewer.remove(o);
				}
				recipeCheckboxTableViewer.refresh();
			}
		});
		btnNewButtonMultiUnchose.setText("<<");
		
		Button btnNewButtonSingleUnchose = new Button(composite_2, SWT.NONE);
		btnNewButtonSingleUnchose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection =(StructuredSelection) recipeCheckboxTableViewer.getSelection();
				if(selection.isEmpty())
					return;
				Object o=selection.getFirstElement();
				MealRecipe recipeItem=(MealRecipe)o;
				FoodMaterial m=recipeItem.getFoodMaterial();
				checkboxTableViewer.add(m);
				recipe.remove(recipeItem);
				recipeCheckboxTableViewer.remove(o);
				recipeCheckboxTableViewer.refresh();
			}
			
		});
		btnNewButtonSingleUnchose.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnNewButtonSingleUnchose.setText("<");
		
		Group group_6 = new Group(group_1, SWT.NONE);
		group_6.setLayout(new BorderLayout(0, 0));
		group_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		recipeCheckboxTableViewer = CheckboxTableViewer.newCheckList(group_6, SWT.BORDER | SWT.FULL_SELECTION);
		tableChosedMaterial = recipeCheckboxTableViewer.getTable();
		tableChosedMaterial.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				StructuredSelection selection =(StructuredSelection) recipeCheckboxTableViewer.getSelection();
				Object o=selection.getFirstElement();
				MealRecipe recipeItem=(MealRecipe)o;
				FoodMaterial m=recipeItem.getFoodMaterial();
				checkboxTableViewer.add(m);
				recipe.remove(recipeItem);
				recipeCheckboxTableViewer.remove(o);
			}
		});
		tableChosedMaterial.setHeaderVisible(true);
		tableChosedMaterial.setLinesVisible(true);
		recipeCheckboxTableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_11 = new TableViewerColumn(recipeCheckboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_11 = tableViewerColumn_11.getColumn();
		tblclmnNewColumn_11.setAlignment(SWT.CENTER);
		tblclmnNewColumn_11.setWidth(150);
		tblclmnNewColumn_11.setText("Material");
		tableViewerColumn_11.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((MealRecipe)element).getFoodMaterial().getName();
			}
			
		});
		TableViewerColumn tableViewerColumn_12 = new TableViewerColumn(recipeCheckboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_12 = tableViewerColumn_12.getColumn();
		tblclmnNewColumn_12.setWidth(100);
		tblclmnNewColumn_12.setText("Qty");
		tableViewerColumn_12.setEditingSupport(new EditingSupport(recipeCheckboxTableViewer){
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				return new TextCellEditor(recipeCheckboxTableViewer.getTable());
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				BigDecimal qty=((MealRecipe)element).getQty();
				return qty.toString();
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				System.out.println("set value "+value.toString());
				((MealRecipe)element).setQty(new BigDecimal(value.toString().equals("")?"0":value.toString()));
				//tableViewer.update(element, null);
				recipeCheckboxTableViewer.refresh();
			}
			
		});
		tableViewerColumn_12.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				BigDecimal qty=((MealRecipe)element).getQty();
				return qty==null?"":qty.toString();
			}
		});
		TableViewerColumn tableViewerColumn_13 = new TableViewerColumn(recipeCheckboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_13 = tableViewerColumn_13.getColumn();
		tblclmnNewColumn_13.setWidth(80);
		tblclmnNewColumn_13.setText("Unit");
		//tableViewerColumn_3.setLabelProvider(new ComboBoxLabelProvider(new String[]{"ff","gg"}));
		tableViewerColumn_13.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				Unit unit=((MealRecipe)element).getUnit();
				return unit==null?"":unit.getName();
			}
		});
		tableViewerColumn_13.setEditingSupport(new EditingSupport(recipeCheckboxTableViewer){
			private ComboBoxViewerCellEditor cellEditor;
			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				cellEditor=new ComboBoxViewerCellEditor(recipeCheckboxTableViewer.getTable());
				cellEditor.setLabelProvider(new LabelProvider(){

					@Override
					public String getText(Object element) {
						// TODO Auto-generated method stub
						return ((Unit)element).getName();
					}
					
				});
		        cellEditor.setContentProvider(new ArrayContentProvider());
		        cellEditor.setInput(unitService.getUnitsByUnitName(null));
				return cellEditor;
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				Unit unit=((MealRecipe)element).getUnit();
				return unit;
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				Unit unit=(Unit)value;
				((MealRecipe)element).setUnit(unit);
				recipeCheckboxTableViewer.refresh();
			}
			
		});
		
		Group group_2 = new Group(parent, SWT.NONE);
		group_2.setLayout(new BorderLayout(0, 0));
		GridData gd_group_2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_group_2.widthHint = 129;
		group_2.setLayoutData(gd_group_2);
		
		tableViewerRni = new TableViewer(group_2, SWT.BORDER | SWT.FULL_SELECTION);
		tableRni = tableViewerRni.getTable();
		tableRni.setLinesVisible(true);
		tableRni.setHeaderVisible(true);
		tableRni.setLayoutData(BorderLayout.CENTER);
		tableViewerRni.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_9 = new TableViewerColumn(tableViewerRni, SWT.NONE);
		TableColumn tblclmnNewColumn_9 = tableViewerColumn_9.getColumn();
		tblclmnNewColumn_9.setWidth(100);
		tblclmnNewColumn_9.setText("Item");
		tableViewerColumn_9.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				FoodIngredientElement ingredientElement= ((Rni)element).getFoodIngredientElement();
				return ingredientElement==null?"":ingredientElement.getName();
			}
			
		});
		TableViewerColumn tableViewerColumn_10 = new TableViewerColumn(tableViewerRni, SWT.NONE);
		TableColumn tblclmnNewColumn_10 = tableViewerColumn_10.getColumn();
		tblclmnNewColumn_10.setWidth(100);
		tblclmnNewColumn_10.setText("Value");
		tableViewerColumn_10.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((Rni)element).getRni());
			}
			
		});
		TableViewerColumn tableViewerColumn_14 = new TableViewerColumn(tableViewerRni, SWT.NONE);
		TableColumn tblclmnNewColumn_14 = tableViewerColumn_14.getColumn();
		tblclmnNewColumn_14.setWidth(100);
		tblclmnNewColumn_14.setText("UL");
		tableViewerColumn_14.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ObjectUtils.toString(((Rni)element).getUl());
			}
			
		});
		TableViewerColumn tableViewerColumn_8 = new TableViewerColumn(tableViewerRni, SWT.NONE);
		TableColumn tblclmnNewColumn_8 = tableViewerColumn_8.getColumn();
		tblclmnNewColumn_8.setWidth(100);
		tblclmnNewColumn_8.setText("Unit");
		tableViewerColumn_8.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				FoodIngredientElement ingredientElement= ((Rni)element).getFoodIngredientElement();
				return ingredientElement==null?"":ingredientElement.getUnit().getName();
			}
			
		});
		Group group_3 = new Group(parent, SWT.NONE);
		group_3.setLayout(new BorderLayout(0, 0));
		group_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		
		canvasResult = new Canvas(group_3, SWT.NONE);
		canvasResult.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				
				GC gc=e.gc;
				Rectangle rectangle=canvasResult.getClientArea();
				if(logger.isDebugEnabled()){
					logger.debug("canvas's client area, point is ( "+rectangle.x+" , "+rectangle.y+" ) , width "+rectangle.width+" , height "+rectangle.height);
				}
				int xMax=rectangle.width;
				int yMax=rectangle.height;
				Padding padding=new Padding(20);
				
				gc.drawString("title", xMax/2, 0);
				gc.drawString("serial",xMax-40,yMax/2);
				gc.drawString("foot note",xMax/2,yMax-20);
				gc.drawRectangle(20, 20, xMax-60, yMax-40);
			}
		});
		
		Button btnNewButton_1 = new Button(parent, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
			}
		});
		GridData gd_btnNewButton_1 = new GridData(SWT.CENTER, SWT.TOP, false, false, 1, 1);
		gd_btnNewButton_1.widthHint = 66;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("Save");
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
